//
//  WYAlertView.m
//  WYAltView
//
//  Created by 王琰 on 14/12/17.
//  Copyright (c) 2014年 王琰. All rights reserved.
//

#import "WYAlertView.h"
//按钮的tag枚举
typedef NS_ENUM(NSUInteger, buttonTags) {
    kCancelButtonTag = 100,
    kConfirmButtonTag = 101,
};

@interface WYAlertView ()
{
    //遮罩层
    UIView *zhezhao;
    //回调block
    TLCompletionBlock cancelCompletionBlock;
    TLCompletionBlock confirmCompletionBlock;
}
@property(strong, nonatomic) UILabel *titleLabel;
@property(strong, nonatomic) UILabel *messageLabel;
@property(strong, nonatomic) UIView *viewToShowIn;
@property(strong, nonatomic) UIButton *confirmButton;
@property(strong, nonatomic) UIButton *cancelButton;

@end
@implementation WYAlertView
//添加自定义的init方法
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message inView:(UIView *)view cancelButtonTitle:(NSString *)cancelButton confirmButton:(NSString *)confirmButton
{
    self = [super initWithFrame:CGRectMake(CGRectGetMinX(view.frame) + 30, CGRectGetMinY(view.frame) - 175, CGRectGetWidth(view.frame) - 60, 150)];
   
    if (self) {
        //标题提示
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.bounds) + 10, CGRectGetMinY(self.bounds) + 14, CGRectGetWidth(self.bounds) - 20, 24)];
        _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.adjustsFontSizeToFitWidth = YES;
        //消息主体
        _messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.bounds) + 10, CGRectGetMaxY(_titleLabel.frame) + 5, CGRectGetWidth(self.bounds) - 20, 48)];
        _messageLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:16];
        _messageLabel.textAlignment = NSTextAlignmentCenter;
        _messageLabel.backgroundColor = [UIColor clearColor];
        _messageLabel.numberOfLines = 0;
        _messageLabel.textColor = [UIColor lightGrayColor];
        _messageLabel.adjustsFontSizeToFitWidth = YES;
        
        //确定，取消按钮
        if(cancelButton && confirmButton) {
            _cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.bounds) + 10, CGRectGetMaxY(self.bounds) - 44, (CGRectGetWidth(self.bounds) / 2) - 20, 30)];
            _cancelButton.backgroundColor = [UIColor clearColor];
            _cancelButton.tag = kCancelButtonTag;
            _cancelButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:16];
            [_cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            _cancelButton.titleLabel.adjustsFontSizeToFitWidth = YES;
            _cancelButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
            
            _confirmButton = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.bounds) - (CGRectGetWidth(self.bounds) / 2) + 10, CGRectGetMaxY(self.bounds) - 44, (CGRectGetWidth(self.bounds) / 2) - 20, 30)];
            //按钮颜色
            _confirmButton.backgroundColor = [UIColor clearColor];
            [_confirmButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            
            _confirmButton.tag = kConfirmButtonTag;
            _confirmButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:16];
            _cancelButton.titleLabel.adjustsFontSizeToFitWidth = YES;
            _confirmButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
        }
        else {
            _cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.bounds) + 10, CGRectGetMaxY(self.bounds) - 54, CGRectGetWidth(self.bounds) - 20, 44)];
            _cancelButton.backgroundColor = [UIColor clearColor];
            _cancelButton.tag = kCancelButtonTag;
            _cancelButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:20];
            _cancelButton.titleLabel.adjustsFontSizeToFitWidth = YES;
            _cancelButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
        }
        //分割线
        UILabel *f1 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMinY(_cancelButton.frame)-12, self.frame.size.width, 1)];
        f1.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self addSubview:f1];
        UILabel *f2 = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width/2, CGRectGetMinY(_cancelButton.frame)-12, 1, 55)];
        f2.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self addSubview:f2];
        
        
        self.layer.edgeAntialiasingMask = kCALayerLeftEdge | kCALayerRightEdge;
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor whiteColor];
        
        _viewToShowIn = view;
        _titleLabel.text = title;
        _messageLabel.text = message;
        [_confirmButton setTitle:confirmButton forState:UIControlStateNormal];
        [_cancelButton setTitle:cancelButton forState:UIControlStateNormal];
        [_confirmButton addTarget:self action:@selector(executeCompletionBlock:) forControlEvents:UIControlEventTouchUpInside];
        [_cancelButton addTarget:self action:@selector(executeCompletionBlock:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:_titleLabel];
        [self addSubview:_messageLabel];
        if(_confirmButton)
            [self addSubview:_confirmButton];
        [self addSubview:_cancelButton];

    }
    
    return self;
}
//获取实例的类方法
+ (instancetype)showInView:(UIView *)view withTitle:(NSString *)title message:(NSString *)message confirmButtonTitle:(NSString *)confirm cancelButtonTitle:(NSString *)cancel
{
    return [[WYAlertView alloc]initWithTitle:title message:message inView:view cancelButtonTitle:cancel confirmButton:confirm];
}

//启动展示
- (void)show
{
    CGRect oldFrame = self.frame;
    oldFrame.origin.y = - self.frame.size.height;
    self.frame = oldFrame;
    zhezhao = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _viewToShowIn.frame.size.width, _viewToShowIn.frame.size.height)];
    zhezhao.backgroundColor = [UIColor blackColor];
    zhezhao.alpha = 0.4;
    [_viewToShowIn addSubview:zhezhao];
    [_viewToShowIn addSubview:self];
   
    [UIView animateWithDuration:0.2 animations:^{
        self.center = CGPointMake(_viewToShowIn.frame.size.width/2, _viewToShowIn.frame.size.height/2);
    } completion:^(BOOL finished) {
        CGRect oldFrame = self.frame;
        oldFrame.origin.y = oldFrame.origin.y - 5;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDelay:0.05];
        self.frame = oldFrame;
        [UIView commitAnimations];
    }];
}

#pragma mark - 对话框消失
- (void)executeCompletionBlock:(UIButton *)sender
{
    switch (sender.tag) {
        case kCancelButtonTag:/************取消按钮的监听事件********/
        {
            [UIView animateWithDuration:0.25 animations:^{
                self.alpha = 0;
                zhezhao.alpha = 0;
            } completion:^(BOOL finished) {
                [zhezhao removeFromSuperview];
                [self removeFromSuperview];
                //执行事件
                cancelCompletionBlock();
            }];
        }
            break;
        case kConfirmButtonTag:/************确定按钮的监听事件********/
        {
            [UIView animateWithDuration:0.25 animations:^{
                self.alpha = 0;
                zhezhao.alpha = 0;
            } completion:^(BOOL finished) {
                [zhezhao removeFromSuperview];
                [self removeFromSuperview];
                //执行事件
                confirmCompletionBlock();
            }];
        }
            break;
        default:
            break;
    }
}

#pragma mark - set completion blocks
//确定，取消按钮的回调方法
- (void)handleCancel:(TLCompletionBlock)cancelBlock handleConfirm:(TLCompletionBlock)confirmBlock{
    cancelCompletionBlock = [cancelBlock copy];
    confirmCompletionBlock = [confirmBlock copy];
}
@end
